package cn.edu.imau.zy.examination.controller;


import cn.edu.imau.zy.common.json.JSONObject;
//import cn.edu.imau.zy.examination.aspect.ExaminationConstant;
import cn.edu.imau.zy.examination.domain.*;
import cn.edu.imau.zy.examination.service.*;
import com.alibaba.druid.pool.vendor.NullExceptionSorter;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.edu.imau.zy.common.annotation.LoginUser;
import cn.edu.imau.zy.common.core.entity.system.SysUser;
import cn.edu.imau.zy.common.core.domain.R;
import cn.edu.imau.zy.common.core.controller.BaseController;

import java.util.Date;
import java.util.List;


/**
 * 考试成绩 提供者
 *
 * @author xsc
 * @date 2021-08-10
 */
@RestController
@RequestMapping("fraction")
public class ElFractionController extends BaseController {

    @Autowired
    private IElFractionService elFractionService;

    @Autowired
    private IElQuService elQuService;

    @Autowired
    private IElQuAnswerService elQuAnswerService;

//    @Autowired
//    private IElPaperService elPaperService;
//
//    @Autowired
//    private IElExamRepoService elExamRepoService;

    private int sum = 0;
    private int count = 0;

    /**
     * 查询${tableComment}
     */
    @GetMapping("get/{id}")
    public ElFraction get(@PathVariable("id") String id) {
        return elFractionService.getById(id);

    }

    /*
     *  查询学生是否参加考试
     */
    @GetMapping("isExam/{id}/{examId}")
    public R isExamination(@PathVariable("id") String id, @PathVariable("examId") String examId) {

        try {
            ElFraction one = elFractionService.getOne(new QueryWrapper<ElFraction>()
                    .eq("user_id", id)
                    .eq("exam_id", examId));
            if (one != null) {
                return R.error(304, "学生已经参加过考试！！");
            }
        } catch (NullPointerException e) {
            return R.ok();
        }

//        return R.error(304,"学生已经参加过考试！！");
        return R.ok();

    }


    /**
     * 查询考试成绩列表
     */
    @GetMapping("list")
    public R list(ElFraction elFraction) {
        startPage();
        return result(elFractionService.queryList(elFraction));
    }


    /**
     * 新增保存考试成绩
     */
//    @PostMapping("save")
//    public R addSave(@RequestBody ElFraction elFraction)  {
//        sum = 0;
//        // 题目的数量
//        ElPaper elPaper = elPaperService.getOne(new QueryWrapper<ElPaper>().eq("exam_id", elFraction.getExamId()));
//        ElExamRepo elExamRepo = elExamRepoService.getById(elPaper.getRepoId());
//        try {
//            elFraction.getQuAnswerListt().forEach(item -> {
//                count = 0;
//                item.forEach(item1 -> {
//                    // 题目内容
//                    ElQu elQu = elQuService.getById(item1.getQuId());
//                    // 拿到正确的答案
//                    List<ElQuAnswer> quList = elQuAnswerService.list(new QueryWrapper<ElQuAnswer>().eq("qu_id", item1.getQuId()).eq("is_right", 1));
//                    // 用户选择答案
//                    ElQuAnswer elQuAnswer = elQuAnswerService.getById(item1.getId());
//
//                    if (elQu.getQuType() == 1L) {
//                        if (elQuAnswer.getIsRight() == 1 && elQuAnswer.getId().equals(quList.get(0).getId())) {
//                            sum += elExamRepo.getRadioScore();
//                        }
//                    } else if (elQu.getQuType() == 3L) {
//                        sum += elExamRepo.getJudgeScore();
//                    } else if (elQu.getQuType() == 4L) {
//                        sum += elExamRepo.getSaqScore();
//                    } else if (elQu.getQuType() == 5L) {
//                        sum += elExamRepo.getProgramScore();
//                    } else if (elQu.getQuType() == 6L) {
//                        sum += elExamRepo.getFillScore();
//                    }
//
//                    if (elQu.getQuType() == 2L) {
//                        for (int i = 0; i < quList.size(); i++) {
//                            if (item.size() == quList.size()) {
//                                if (elQuAnswer.getIsRight() == 1 && elQuAnswer.getId().equals(quList.get(i).getId())) {
//                                    count++;
//                                }
//                            }
//                        }
//                        if (count == item.size()) {
//                            sum += elExamRepo.getMultiScore();
//                        }
//                    }
//                });
//            });
//        } catch (NullPointerException e){
//            sum = sum + 0;
//        }
//
//
//
//        elFraction.setFraction(sum);
//        elFraction.setState(1);
//        elFraction.setCreateTime(new Date());
//        elFraction.setUpdateTime(new Date());
//        elFractionService.save(elFraction);
//
//        return R.ok().put("fraction", sum);
//    }

    /**
     * 修改保存考试成绩
     */
    @PostMapping("update")
    public R editSave(@RequestBody ElFraction elFraction, @LoginUser SysUser user) {
        return toAjax(elFractionService.updateById(elFraction));
    }

    /**
     * 删除${tableComment}
     */
    @PostMapping("remove")
    public R remove(String ids) {
        return toAjax(elFractionService.removeById(ids));
    }

}
